﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using FPC.Offer.Common;
using System.IO;
namespace FPC.Offer.UI.GeneralProcessor.Power
{
    /// <summary>
    /// HandlerPower 的摘要说明
    /// </summary>
    public class HandlerPower : IHttpHandler
    {
        FPC.Offer.Service.System_PowerTableService Power = new Service.System_PowerTableService();
        FPC.Offer.Model.System_PowerTable mo = new Model.System_PowerTable();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var Actioon = context.Request["Actioon"].ToString();
            //查询
            if (Actioon == EnumAction.Select.ToString())
            {
                if (context.Request["GetName"] != null)
                {
                    if (context.Request["GetName"].ToString() == "1")
                    {
                        context.Response.Write(JsonConvert.SerializeObject(Power.Find(t => t.PowerSublevelId == 0)));
                    }
                    else if (context.Request["GetName"].ToString() =="0" )
                    {
                        context.Response.Write(JsonConvert.SerializeObject(Power.Find(null)));                       
                    }
                    else
                    {
                        var name = context.Request["GetName"].ToString();
                        context.Response.Write(JsonConvert.SerializeObject(Power.PowerLikeShow(name)));                       
                    }
                }
                else if (context.Request["GetId"] != null)
                {
                    var Getid = Convert.ToInt32(context.Request["GetId"].ToString());
                    context.Response.Write(JsonConvert.SerializeObject(Power.Find(t => t.PowerId == Getid)));
                }              
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(Power.Find(null)));
                }
              
                context.Response.Write("");
            }
            //添加
            if (Actioon == EnumAction.Add.ToString())
            {
                if (context.Request["PowerName"] != null && context.Request["PowerRemarks"] != null && context.Request["PowerAddress"] != null)
                {
                    mo.PowerName = context.Request["PowerName"].ToString();
                    mo.PowerRemarks = context.Request["PowerRemarks"].ToString();
                    mo.PowerAddress = context.Request["PowerAddress"].ToString();

                    if (context.Request["PowerSublevelId"] == null)
                    {
                        mo.PowerSublevelId = 0;
                    }
                    else
                    {
                        mo.PowerSublevelId = Convert.ToInt32(context.Request["PowerSublevelId"].ToString());
                    }
                    context.Response.Write(Power.Add(mo));
                }
                context.Response.Write("");
            }
            //修改
            if (Actioon == EnumAction.Update.ToString())
            {
                if (context.Request["PowerName"] != null && context.Request["PowerRemarks"] != null && context.Request["PowerAddress"] != null && context.Request["PowerId"] != null && context.Request["PowerSublevelId"] != null)
                {                  
                    mo.PowerName = context.Request["PowerName"].ToString();
                    mo.PowerRemarks = context.Request["PowerRemarks"].ToString();
                    mo.PowerAddress = context.Request["PowerAddress"].ToString();                   
                    mo.PowerSublevelId = Convert.ToInt32(context.Request["PowerSublevelId"].ToString());
                    mo.PowerId = Convert.ToInt32(context.Request["PowerId"].ToString());
                    context.Response.Write(Power.Update(mo));
                }
            }
            //删除
            if (Actioon == EnumAction.Delete.ToString())
            {
                if (context.Request["PowerDelete"] != null)
                {
                    if (Convert.ToInt32(context.Request["PowerDelete"].ToString()) > 1)
                    {
                        var GetId = context.Request["PowerDelete"].ToString();
                        context.Response.Write(Power.PowerBatchDelete(GetId));
                    }
                    else 
                    {
                        mo.PowerId = Convert.ToInt32(context.Request["PowerDelete"].ToString());
                        context.Response.Write(Power.Delete(mo));
                    }                   
                }
            }
            context.Response.Write("");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}